﻿package com.toncen.samepms.net.dao;

import java.util.List;

import com.toncen.samepms.common.AbstractDao;
import com.toncen.samepms.common.IDomain;
import com.toncen.samepms.net.domain.Channel;
import com.toncen.samepms.net.domain.RoleChannel;

/**
 * 描述：栏目表NET_CHANNEL的dao文件
 * @time 2013-5-10
 * @author xyp
 */
public class ChannelDao extends AbstractDao<Channel> {

	public ChannelDao() {
	}

	/*
	 * (non-Javadoc)
	 * 级联增加栏目角色
	 * @see com.toncen.samepms.common.AbstractDao#add(com.toncen.samepms.common.IDomain)
	 */
	@Override
	public long add(Channel domain) throws Exception {
		long id = super.add(domain);
		RoleChannelDao roleChannelDao = new RoleChannelDao();
		for(RoleChannel roleChannel: domain.getRoleChannels()){ //for重新设置关联关系
			roleChannel.setId(IDomain.UNSAVED_ID);
			roleChannel.setChannel(domain);
		}
		roleChannelDao.add(domain.getRoleChannels());
		return id;
	}


	/* (non-Javadoc)
	 * 级联修改栏目角色
	 * @see com.toncen.samepms.common.AbstractDao#update(com.toncen.samepms.common.IDomain)
	 */
	@Override
	public int update(Channel domain) throws Exception {
		for (RoleChannel roleChannel : domain.getRoleChannels()) {// 防止延迟加载没有加载就删除了
			roleChannel.getChannel();
		}
		RoleChannelDao roleChannelDao = new RoleChannelDao();
		Channel oldDomain = find(domain);
		if (oldDomain != null) {
			roleChannelDao.deleteByChannelId(domain.getId()); //根据栏目ID删除角色栏目
		}
		roleChannelDao.add(domain.getRoleChannels());
		return super.update(domain);
	}

	/*
	 * (non-Javadoc)
	 * 级联删除栏目角色
	 * @see com.toncen.samepms.common.AbstractDao#delete(long)
	 */
	@Override
	public int delete(long id) throws Exception {
		RoleChannelDao roleChannelDao = new RoleChannelDao();
		roleChannelDao.deleteByChannelId(id);
		return super.delete(id);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.comit.common.db.BaseDao#initialize()
	 */
	@Override
	protected void initialize() {
		setStatementPrefix("Channel");
		setStatementDivision(".");
	}

	/**
	 * 获取一级根栏目
	 * 
	 * @param exclude
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public List<Channel> getAllRootChannel() throws Exception {
		return findList("getAllRootChannel", null);
	}
	
	/**
	 * 获取一级根栏目
	 * 
	 * @param exclude
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public List<Channel> getAllRootChannelByChannelId(long channelId) throws Exception {
		return findList("getAllRootChannelByChannelId", channelId);
	}
	
	/**
	 * 更新最后发表时间
	 * 
	 * @param exclude
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public void updateLastWriteTime() throws Exception {
		getSqlMap().update(getStatement("updateLastWriteTime"));
		
	}
	
	/**
	 * 根据状态获取栏目
	 * 
	 * @param exclude
	 * @return
	 * @throws Exception
	 */
	public List<Channel> getAllByState(int state) throws Exception{
		return findList("getAllByState", state);
	}
	/**
	 * 级联加载父栏目
	 * @param channel
	 * @throws Exception
	 */
	public void loadCascadeParent(Channel channel) throws Exception{
		if(channel.getParent()!=null&&channel.getParent().isSaved()){
			channel.setParent(load(channel.getParent()));
			loadCascadeParent(channel.getParent());
		}
	}
}
